﻿using System;
using System.Linq;
using System.Windows;

namespace SvgViewer.Utilities
{
    /// <summary>
    /// 语言类别
    /// </summary>
    public enum Languages
    {
        /// <summary>
        /// 中文
        /// </summary>
        CN = 0,

        /// <summary>
        /// 英文
        /// </summary>
        EN
    }

    internal class LanguageManager
    {
        public static readonly LanguageManager Instance = new();

        private LanguageManager() { }

        private const string LangUri = "pack://application:,,,/SvgViewer;component/Langs/{0}.xaml";

        private readonly string[] Langs = {
            "cn", "en"
        };

        /// <summary>
        /// 切换语言
        /// </summary>
        /// <param name="lang">要切换的语言类别</param>
        public void ChangeLanguage(Languages lang)
        {
            var langName = Langs[(int)lang];
            foreach (var item in Application.Current.Resources.MergedDictionaries)
            {
                if (IsLanguageUri(item.Source?.AbsoluteUri))
                {
                    item.Source = new Uri(string.Format(LangUri, langName), UriKind.RelativeOrAbsolute);
                    break;
                }
            }
        }

        private bool IsLanguageUri(string uri)
        {
            if (string.IsNullOrEmpty(uri))
            {
                return false;
            }

            return Langs.Any(lang => uri == string.Format(LangUri, lang));
        }

        /// <summary>
        /// 获取资源Key对应的内容
        /// </summary>
        /// <param name="resourceKey">资源Key</param>
        /// <returns>获取成功返回对应的内容，失败返回 string.Empty</returns>
        public string GetText(string resourceKey)
        {
            if (string.IsNullOrEmpty(resourceKey))
            {
                return string.Empty;
            }

            var index        = resourceKey.LastIndexOf(".", StringComparison.CurrentCulture);
            var prefix       = string.Empty;
            var searchPrefix = false;
            if (index == -1)
            {
                prefix       = resourceKey.Substring(0, index + 1);
                searchPrefix = true;
            }

            foreach (var item in Application.Current.Resources.MergedDictionaries)
            {
                if (IsLanguageUri(item.Source?.AbsoluteUri) == false)
                {
                    continue;
                }

                foreach (var dict in item.MergedDictionaries)
                {
                    if (searchPrefix && dict.Source.AbsoluteUri.IndexOf(prefix, StringComparison.CurrentCulture) ==
                        -1)
                    {
                        continue;
                    }

                    foreach (var key in dict.Keys)
                    {
                        if ($"{key}" == resourceKey)
                        {
                            return $"{dict[key]}";
                        }
                    }
                }
            }

            return string.Empty;
        }
    }
}